﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PlotExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            float xMin = -180, xMax = 180, xStep = 30;
            float yMin = -150, yMax = -50, yStep = 20;
            float referValue = -107;
            int N = (int)((xMax - xMin) / xStep);

            float[] x = new float[N];
            float[] y = new float[N];
            Random random1 = new Random();
            for (int i = 0; i < N; i++)
            {
                x[i] = i * xStep - 180;
                int min = (int)(yMin + 0.5 * (yMax - yMin));
                y[i] = random1.Next(min, (int)yMax);
                
            }
            //
            // 直角坐标
            //
            plot1.AxisX.Minimum = xMin;   // X轴最小值
            plot1.AxisX.Maximum = xMax;   // X轴最大值
            plot1.AxisX.Interval = xStep;
            plot1.AxisY.Minimum = yMin;// Y轴最小值
            plot1.AxisY.Interval = yStep;
            plot1.AxisY.Maximum = yMax; // Y轴最大值
            plot1.ReferLineValue = referValue;
            plot1.ReferLineColor = Color.Red;
            plot1.PickEnabled = true;

            Charting.Series series = plot1.NewSeries();
            series.LineColor = Color.Green;
            series.LineWidth = 1.8F;
            series.IsValueShownAsLabel = true;
            series.PointSize = 10;
            series.PointColor = Color.Blue;
            series.LabelBackColor = Color.Blue;

           
            series.BeginUpdate();
            for (int i = 0; i < N; i++)
            {
                series.Points.Add(x[i], y[i]);
            }
            series.EndUpdate();
            //
            // 极坐标
            //
            plot2.AxisX.Minimum = xMin;   // X轴最小值
            plot2.AxisX.Maximum = xMax;   // X轴最大值
            plot2.AxisX.Interval = xStep;
            plot2.AxisY.Minimum = yMin;// Y轴最小值
            plot2.AxisY.Interval = yStep;
            plot2.AxisY.Maximum = yMax; // Y轴最大值
            plot2.ReferLineValue = referValue;
            plot2.ReferLineColor = Color.Red;
            plot2.PickEnabled = true;

            Charting.Series series1 = plot2.NewSeries();
            series1.LineColor = Color.Green;
            series1.LineWidth = 1.8F;
            series1.IsValueShownAsLabel = true;
            series1.PointSize = 10;
            series1.PointColor = Color.Blue;
            series1.LabelBackColor = Color.Blue;

            
            series1.BeginUpdate();
            for (int i = 0; i < N; i++)
            {
                series1.Points.Add(x[i], y[i]);
            }
            series1.EndUpdate();
        }

        private void Plot1_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                Charting.DataPoint[] dp = plot1.GetCursorPoint(e.X, e.Y, true);
                lbCartesian.Text = $"x = {dp[0].X}, y = {dp[0].Y}";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Plot2_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                Charting.DataPoint[] dp = plot2.GetCursorPoint(e.X, e.Y, true);
                lbPolar.Text = $"x = {dp[0].X}, y = {dp[0].Y}";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.ShowDialog();
        }
    }
}
